package com.pz.cloud.framework.leagueMem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.cloud.framework.base.dao.CommonDao;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMem;
import com.pz.cloud.framework.business.leagueMem.service.ILeagueMemService;
import com.pz.cloud.framework.business.student.entity.Student;
import com.pz.cloud.framework.leagueMem.mapper.LeagueMemMapper;
import com.pz.cloud.framework.model.AbstractSqlBuilder;
import com.pz.cloud.framework.model.SqlBuilder;
import com.pz.cloud.framework.util.BizUtil;
import org.jeecg.common.handler.DataScopeHandler;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Description: 团员
 * @Author: jeecg-boot
 * @Date:   2020-11-28
 * @Version: V1.0
 */
@Service
@Transactional
public class LeagueMemServiceImpl extends ServiceImpl<LeagueMemMapper, LeagueMem> implements ILeagueMemService {

    @Autowired
    private CommonDao commonDao;

    @Override
    public IPage<LeagueMem> findPage(LeagueMem entity, Map<String, String[]> parameterMap) {
        return commonDao.findPage(sqlBuilder(entity, parameterMap));
    }

    @Override
    public List<LeagueMem> list(LeagueMem entity, Map<String, String[]> parameterMap) {
        return commonDao.findList(sqlBuilder(entity, parameterMap));
    }

    private AbstractSqlBuilder sqlBuilder(LeagueMem entity, Map<String, String[]> parameterMap) {

        AbstractSqlBuilder sqlBuilder = SqlBuilder.init("select lm.*,u.username std_no,u.realname,u.sex,s.college,s.major,s.grade,s.clazz from ")
                .append(entity.getTableName()).append(" lm")
                .append(" inner join ").append(BizUtil.getTableName(SysUser.class)).append(" u on u.id = lm.user_id and u.del_flag = 0 ")
                .append(" left join ").append(BizUtil.getTableName(Student.class)).append(" s on s.user_id = lm.user_id and s.del_flag = 0 ")
                .returnType(LeagueMem.class)
                .page(entity)
                .joinCondition(entity,"lm", parameterMap)
                .filterDataScope("lm","user_id");

        if (entity.getStdNo() != null) {
            sqlBuilder.append(" and u.username like :stdNo").setParmsLike("stdNo", entity.getStdNo());
        }
        if (entity.getRealname() != null) {
            sqlBuilder.append(" and u.realname like :realname").setParmsLike("realname", entity.getRealname());
        }
        return sqlBuilder;
    }

    @Override
    public LeagueMem getByUserId(String userId) {
        LambdaQueryWrapper<LeagueMem> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(LeagueMem::getUserId, userId);
        List<LeagueMem> list = list(wrapper);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

}
